﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class TransferBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public TransferBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string SerialNo)
        {
            if (DAO.T_TRANSFERs.Where(t => t.SERIAL_NO == Convert.ToInt32(SerialNo)).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_TRANSFER> GetTransferList()
        {
            var list = DAO.T_TRANSFERs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_TRANSFER GetTransferByTransferID(string TransferID)
        {
            return DAO.T_TRANSFERs.Where(t => t.TRANSFER_ID.ToString() == TransferID).SingleOrDefault();
        }

        public int AddTransfer(T_TRANSFER Transfer)
        {
            try
            {
                Transfer.TRANSFER_ID = DAO.GetGUID();

                DAO.T_TRANSFERs.InsertOnSubmit(Transfer);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddTransfer(T_TRANSFER Transfer, out string TransferGuid)
        {
            try
            {
                Transfer.TRANSFER_ID = DAO.GetGUID();
                
                DAO.T_TRANSFERs.InsertOnSubmit(Transfer);
                DAO.SubmitChanges();

                TransferGuid = Transfer.TRANSFER_ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                TransferGuid = "";
                return -1;
            }
        }

        public int UpdateTransfer(T_TRANSFER Transfer)
        {
            try
            {
                T_TRANSFER updateEntity = DAO.T_TRANSFERs.SingleOrDefault(t=>t.TRANSFER_ID.ToString()==Transfer.TRANSFER_ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.AUDIT_TIME = Transfer.AUDIT_TIME;
                updateEntity.AUDITOR_ID = Transfer.AUDITOR_ID;
                updateEntity.CREATE_TIME = Transfer.CREATE_TIME;
                updateEntity.CREATOR_ID = Transfer.CREATOR_ID;
                updateEntity.STATE = Transfer.STATE;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteTransfer(string TransferID)
        {
            try
            {
                T_TRANSFER deleteEntity = DAO.T_TRANSFERs.SingleOrDefault(t => t.TRANSFER_ID.ToString() == TransferID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_TRANSFER_DETAILs.DeleteAllOnSubmit(deleteEntity.T_TRANSFER_DETAILs);
                DAO.T_TRANSFERs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteTransfers(List<string> TransferIDs)
        {
            try
            {
                foreach (string TransferID in TransferIDs)
                {
                    T_TRANSFER deleteEntity = DAO.T_TRANSFERs.SingleOrDefault(t => t.TRANSFER_ID.ToString() == TransferID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_TRANSFER_DETAILs.DeleteAllOnSubmit(deleteEntity.T_TRANSFER_DETAILs);
                    DAO.T_TRANSFERs.DeleteOnSubmit(deleteEntity);
                }
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AuditTransfer(string ID, Guid UserID,out string Message)
        {
            try
            {
                Message = string.Empty;
                DAO.BeginTransaction();
                T_TRANSFER entity = DAO.T_TRANSFERs.SingleOrDefault(t => t.TRANSFER_ID.ToString() == ID);

                entity.STATE = (int)Enumerations.StockStatus.审核通过;
                entity.AUDIT_TIME = DateTime.Now;
                entity.AUDITOR_ID = UserID;

                foreach (T_TRANSFER_DETAIL item in entity.T_TRANSFER_DETAILs)
                {
                    if (item.FROM_FLOOR_ID.ToString() != item.TO_FLOOR_ID.ToString())
                    {
                        T_STORE storeFrom = DAO.T_STOREs.SingleOrDefault(t => t.PRODUCT_DETAIL_ID.ToString() == item.PRODUCT_DETAIL_ID.ToString()
                                                                        && t.FloorID.ToString() == item.FROM_FLOOR_ID.ToString());
                        double OldGorssWeight = storeFrom.GROSS_WEIGHT;
                        double OldNet_Weight = storeFrom.NET_WEIGHT;
                        double OldPacketQty = storeFrom.PACKET_QTY;
                        double OldPacketQty2 = storeFrom.PACKET_QTY2;

                        storeFrom.GROSS_WEIGHT -= (double)item.GROSS_WEIGHT;
                        storeFrom.NET_WEIGHT -= (double)item.NET_WEIGHT;
                        storeFrom.PACKET_QTY -= (double)item.PACKET_QTY;
                        storeFrom.PACKET_QTY2 -= (double)item.PACKET_QTY2;




                        if (storeFrom.GROSS_WEIGHT < 0)
                        {
                            DAO.RollBack();
                            Message = "库存中的" + storeFrom.T_PRODUCT.NAME + "的毛重只有" + Convert.ToString(OldGorssWeight) + ",不足移仓需要的" + item.GROSS_WEIGHT;
                            return -1;
                        }
                        if (storeFrom.NET_WEIGHT < 0)
                        {
                            DAO.RollBack();
                            Message = "库存中的" + storeFrom.T_PRODUCT.NAME + "的净重只有" + Convert.ToString(OldNet_Weight) + ",不足移仓需要的" + item.NET_WEIGHT;
                            return -2;
                        }
                        if (storeFrom.PACKET_QTY < 0)
                        {
                            DAO.RollBack();
                            Message = "库存中的" + storeFrom.T_PRODUCT.NAME + "的大件数只有" + Convert.ToString(OldPacketQty) + ",不足移仓需要的" + item.PACKET_QTY;
                            return -3;
                        }
                        if (storeFrom.PACKET_QTY2 < 0)
                        {
                            DAO.RollBack();
                            Message = "库存中的" + storeFrom.T_PRODUCT.NAME + "的小件数只有" + Convert.ToString(OldPacketQty2) + ",不足移仓需要的" + item.PACKET_QTY2;
                            return -4;
                        }

                        T_STORE storeTo = DAO.T_STOREs.SingleOrDefault(t => t.PRODUCT_DETAIL_ID.ToString() == item.PRODUCT_DETAIL_ID.ToString()
                                                                        && t.FloorID.ToString() == item.TO_FLOOR_ID.ToString());
                        if (storeTo != null)
                        {
                            storeTo.GROSS_WEIGHT += (double)item.GROSS_WEIGHT;
                            storeTo.NET_WEIGHT += (double)item.NET_WEIGHT;
                            storeTo.PACKET_QTY += (double)item.PACKET_QTY;
                            storeTo.PACKET_QTY2 += (double)item.PACKET_QTY2;
                        }
                        else
                        {
                            T_STORE storeNew = new T_STORE();
                            storeNew.ID = DAO.GetGUID();
                            storeNew.FloorID = item.TO_FLOOR_ID;
                            storeNew.PRODUCT_DETAIL_ID = (Guid)item.PRODUCT_DETAIL_ID;
                            storeNew.PRODUCT_ID = item.T_PRODUCT_DETAIL.PRODUCT_ID;
                            storeNew.GROSS_WEIGHT = (double)item.GROSS_WEIGHT;
                            storeNew.NET_WEIGHT = (double)item.NET_WEIGHT;
                            storeNew.PACKET_QTY = (double)item.PACKET_QTY;
                            storeNew.PACKET_QTY2 = (double)item.PACKET_QTY2;
                            DAO.T_STOREs.InsertOnSubmit(storeNew);
                        }

                        DAO.SubmitChanges();
                    }
                }
                DAO.SubmitChanges();
                DAO.Commit();
                return 1;
            }
            catch (Exception ex)
            {
                Message = string.Empty;
                return -1;
            }
        }

        public int BackTransfer(string ID)
        {
            try
            {
                T_TRANSFER entity = DAO.T_TRANSFERs.SingleOrDefault(t => t.TRANSFER_ID.ToString() == ID);

                entity.STATE = (int)Enumerations.StockStatus.打回;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public bool IsAudited(string ID)
        {
            T_TRANSFER entity = DAO.T_TRANSFERs.SingleOrDefault(t => t.TRANSFER_ID.ToString() == ID);

            return entity.STATE == (int)Enumerations.StockStatus.审核通过 ? true : false;
        }
    }
}
